package hu.mapro.rollerx.core;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.joints.WheelJointDef;

public class Car1 implements Car {


	// Car
	
	private static final float CAR_WIDTH = 6.0f;
	private static final float CAR_PLATFORM_HEIGHT = 0.5f;
	private static final float CAR_WALL_HEIGHT = 0.5f;
	private static final float CAR_WALL_WIDTH = 0.5f;
	private static final float CAR_WHEEL_RADIUS = 1.0f;
	private static final float CAR_WHEEL_DISTANCE = CAR_WIDTH-CAR_WHEEL_RADIUS*2;
	private static final float CAR_WHEEL_SUSPENSION = 0.75f;
	
	private static final float CAR_PLATFORM_BOTTOM = CAR_WHEEL_RADIUS + CAR_WHEEL_SUSPENSION;
	private static final float PLATFORM_LOAD_HEIGHT = CAR_PLATFORM_BOTTOM + CAR_PLATFORM_HEIGHT;
	
	public Body carBody;
	private Body wheel1Body;
	private Body wheel2Body;

	private float m_hz;
	private float m_zeta;

	public static final float CAR_HEIGHT = CAR_WHEEL_RADIUS + CAR_WHEEL_SUSPENSION + CAR_PLATFORM_HEIGHT + CAR_WALL_HEIGHT;
	
	public Fixture carPlatformFixture;

	@Override
	public void init(RollerWorld rollerWorld, Vector2 carPosition) {
		World world = rollerWorld.world;
		
		// - car
		
	    m_hz = 4.0f;
	    m_zeta = 0.7f;
		
		PolygonShape carPlatformShape = new PolygonShape();
		Vector2 carPlatformVertices[] = new Vector2[] {
				new Vector2(-CAR_WIDTH/2.0f, CAR_PLATFORM_BOTTOM),
				new Vector2(CAR_WIDTH/2.0f, CAR_PLATFORM_BOTTOM),
				new Vector2(CAR_WIDTH/2.0f, PLATFORM_LOAD_HEIGHT),
				new Vector2(-CAR_WIDTH/2.0f, PLATFORM_LOAD_HEIGHT),
		};
		carPlatformShape.set(carPlatformVertices);

		PolygonShape carLeftWallShape = new PolygonShape();
		Vector2 carLeftWallVertices[] = new Vector2[] {
				new Vector2(-CAR_WIDTH/2.0f, CAR_PLATFORM_BOTTOM),
				new Vector2(-CAR_WIDTH/2.0f, PLATFORM_LOAD_HEIGHT + CAR_WALL_HEIGHT),
				new Vector2(-CAR_WIDTH/2.0f+CAR_WALL_WIDTH, PLATFORM_LOAD_HEIGHT + CAR_WALL_HEIGHT),
				new Vector2(-CAR_WIDTH/2.0f+CAR_WALL_WIDTH, CAR_PLATFORM_BOTTOM),
		};
		carLeftWallShape.set(carLeftWallVertices);
		
		PolygonShape carRightWallShape = new PolygonShape();
		Vector2 carRightWallVertices[] = new Vector2[] {
				new Vector2(CAR_WIDTH/2.0f, CAR_PLATFORM_BOTTOM),
				new Vector2(CAR_WIDTH/2.0f, PLATFORM_LOAD_HEIGHT + CAR_WALL_HEIGHT),
				new Vector2(CAR_WIDTH/2.0f-CAR_WALL_WIDTH, PLATFORM_LOAD_HEIGHT + CAR_WALL_HEIGHT),
				new Vector2(CAR_WIDTH/2.0f-CAR_WALL_WIDTH, CAR_PLATFORM_BOTTOM),
		};
		carRightWallShape.set(carRightWallVertices);
		
		CircleShape circle = new CircleShape();
		circle.setRadius(CAR_WHEEL_RADIUS);

		
		BodyDef bd = new BodyDef();
		bd.type = BodyType.DynamicBody;
		bd.position.set(carPosition);
		carBody = world.createBody(bd);
		
		FixtureDef carFixtureDef = new FixtureDef();
		carFixtureDef.density = 1.0f;
		carFixtureDef.filter.categoryBits = RollerWorld.COLLISION_CAR;
		carFixtureDef.filter.maskBits = RollerWorld.COLLISION_CARGO | RollerWorld.COLLISION_GROUND;
		
		
		carFixtureDef.shape = carPlatformShape;
		carPlatformFixture = carBody.createFixture(carFixtureDef);
		
		carFixtureDef.shape = carLeftWallShape;
		carPlatformFixture = carBody.createFixture(carFixtureDef);
		carFixtureDef.shape = carRightWallShape;
		carPlatformFixture = carBody.createFixture(carFixtureDef);

		FixtureDef fd = new FixtureDef();
		fd.shape = circle;
		fd.density = 1.0f;
		fd.friction = 0.9f;
		fd.filter.categoryBits = RollerWorld.COLLISION_CAR;
		fd.filter.maskBits = RollerWorld.COLLISION_GROUND | RollerWorld.COLLISION_CARGO;

		bd.position.set(carPosition.x-CAR_WHEEL_DISTANCE/2.0f, carPosition.y + CAR_WHEEL_RADIUS);
		wheel1Body = world.createBody(bd);
		wheel1Body.createFixture(fd);

		bd.position.set(carPosition.x+CAR_WHEEL_DISTANCE/2.0f, carPosition.y + CAR_WHEEL_RADIUS);
		wheel2Body = world.createBody(bd);
		wheel2Body.createFixture(fd);

		WheelJointDef jd = new WheelJointDef();
		Vector2 axis = new Vector2(0.0f, 1.0f);

		jd.initialize(carBody, wheel1Body, wheel1Body.getPosition(), axis);
		jd.motorSpeed = 0.0f;
		jd.enableMotor = false;
		jd.frequencyHz = m_hz;
		jd.dampingRatio = m_zeta;
		try {
			world.createJoint(jd);
		} catch (RuntimeException e) {}

		jd.initialize(carBody, wheel2Body, wheel2Body.getPosition(), axis);
		jd.motorSpeed = 0.0f;
		jd.enableMotor = false;
		jd.frequencyHz = m_hz;
		jd.dampingRatio = m_zeta;
		try {
			world.createJoint(jd);
		} catch (RuntimeException e) {}
		
		carPlatformShape.dispose();
		carRightWallShape.dispose();
		carLeftWallShape.dispose();
		circle.dispose();
		
	}

	@Override
	public float getWidth() {
		return CAR_WIDTH;
	}

	@Override
	public float getHeigth() {
		return CAR_HEIGHT;
	}

	@Override
	public float getPlatformHeight() {
		return PLATFORM_LOAD_HEIGHT;
	}

	@Override
	public Vector2 getPosition() {
		return carBody.getWorldPoint(new Vector2(0, PLATFORM_LOAD_HEIGHT));
	}

	@Override
	public void dispose() {
	}

	
}
